myApp.controller('plan_GuideController', function ($scope, $uibModal, $stateParams, $http, ngDialog) {
    $scope.info = {};
    $scope.info.planType = "0";
    if ($stateParams.type == "1") {
        $scope.info.planType = "1";
    }

    $scope.info.index = 1;
    $scope.info.PlanInfoByUrl = "";
    $scope.info.PlanInfo = [];
    $scope.info.getTrunkProjectList = [];
    $scope.info.checkplan_code = '';
    $scope.info.trunk_list = [];
    $scope.info.ProjectDefaultNameList = [];
    $scope.info.ZoneList = [];
    $scope.info.userName = getUserName();
    $scope.info.zones = [];
    $scope.info.titles = "";
    $scope.info.title_list = [];
    $scope.info.load_level = [];
    $scope.info.agent_List = [];
    $scope.info.integration_date = "";
    $scope.info.online_date = "";
    $scope.info.expect_recover_date = "";
    $scope.info.teamMembers = [];
    $scope.info.one = false;
    $scope.info.realnames = "";
    // $scope.info.ProjectPlanCodelock=false;
    $scope.info.integrationLock = false;
    $scope.info.online_dateLock = false;
    $scope.info.branch_numLock = false;
    $scope.info.nextLock = false;
    $scope.info.DEV = true;
    $scope.info.QA = true;
    $scope.info.UAT = false;
    $scope.info.isUseWeb = false;
    $scope.info.useOldEnv = false;
    $scope.info.useWebDesc = '';
    $scope.info.codeMsg = '';

    $scope.info.emergencyDesc = "";
    $scope.info.bug_num = "";
    $scope.info.need_num = "";

    $scope.info.testUserInfo = {};
    $scope.info.testUserList = [];

    $scope.dateOptions = {
        minDate: new Date()
    };

    doBeetleGet($http,$uibModal,python_url + '/api/v1/user/?role_name=qa&token_id=' + getTokenId(),function (data) {
        $scope.info.testUserList = data.data;
        $scope.info.testUserInfo =$scope.info.testUserList[0];
    });

    $http.get(beetle_plan_url + '/project/getTrunkProjectList?token_id=' + getTokenId()).success(function (data) {
        $scope.info.getTrunkProjectList = data.data;
    });
    $scope.info.getTrunkProjectList.istrue = false;

    $scope.nextBtn = function () {
        var count = 0;

        if ($scope.info.planType != "1") {
            if ($scope.info.one == false) {
                openDialogAlert(ngDialog, $scope, "请输入禅道项目号，url或者编码并进行查询!");
                return false;
            }
            if ($scope.info.integration_date == "" || $scope.info.integration_date == undefined) {
                openDialogAlert(ngDialog, $scope, "请选择集成日期!");
                return false;
            }
            if ($scope.info.integration_date > $scope.info.online_date) {
                openDialogAlert(ngDialog, $scope, "集成日期大于上线日期,请重新选择!");
                return false;
            }
        } else {
            if ($scope.info.PlanInfo.name == undefined || $scope.info.PlanInfo.name == "") {
                openDialogAlert(ngDialog, $scope, "紧急上线项目名不能为空!");
                return false;
            }
            if ($scope.info.emergencyDesc == "") {
                openDialogAlert(ngDialog, $scope, "紧急上线描述不能为空!");
                return false;
            }
            if ($scope.info.bug_num == "" && $scope.info.need_num == "") {
                openDialogAlert(ngDialog, $scope, "bug号和需求号必须填一个!");
                return false;
            }
            if($scope.info.testUserInfo == null || $scope.info.testUserInfo.account == undefined){
                openDialogAlert(ngDialog, $scope, "测试负责人不能为空!");
                return false;
            }

            if($scope.info.expect_recover_date == ''){
                $scope.info.expect_recover_date = $scope.info.online_date;
            }
        }

        if ($scope.info.online_date == "" || $scope.info.online_date == undefined) {
            openDialogAlert(ngDialog, $scope, "请选择上线日期!");
            return false;
        }

        if ($scope.info.index == 2) {
            $scope.info.trunk_list = [];
            angular.forEach($scope.info.getTrunkProjectList.jarProject, function (data) {
                if (data.istrue == true) {
                    $scope.info.trunk_list.push(data.project_name);
                }
            });
            angular.forEach($scope.info.getTrunkProjectList.trunkProject, function (data) {
                if (data.istrue == true) {
                    $scope.info.trunk_list.push(data.project_name);
                }
            });
            $scope.info.ProjectDefaultNameList = [];
            $scope.getPlanProjectDefaultName();
            $scope.testCode();
        }
        if ($scope.info.index == 3) {

            if ($scope.info.checkplan_code == '') {
                alert('请输入项目代号');
                return false;
            }
            $scope.getZoneList();
            $scope.getLoad_level();
        }

        if ($scope.info.index == 4) {
            if ($scope.info.DEV || $scope.info.QA || $scope.info.UAT) {
                if ($scope.info.expect_recover_date == "" || $scope.info.expect_recover_date == undefined) {
                    alert('请选择回收日期！');
                    return false;
                }
            }
            $scope.info.titles = "";
            $scope.info.title_list = [];
            angular.forEach($scope.info.zones, function (data) {
                $scope.info.titles += data.zone_title + " ";
                $scope.info.title_list.push(data.zone_title);
            });
            if ($scope.info.title_list.length == 0) {
                alert('至少选择一个环境区域！');
                return false
            }
        }
        angular.forEach($scope.info.zones, function (zone) {

            if (zone.available) {
                count = 1;
                return;
            }
        });
        if ($scope.info.index < 5) {
            if($scope.info.planType == '1' && $scope.info.index == 3){
                $scope.info.DEV = false;
                $scope.info.QA = false;
                $scope.info.UAT = false;
                $scope.info.index = 5;
            }else {
                $scope.info.index = $scope.info.index + 1;
            }
        } else {
            $scope.info.index = 5;
        }
    };

    $scope.updateIntegrationDate = function () {
        if ($scope.info.PlanInfo.length <= 0) {
            $scope.info.integration_date = "";
            $scope.info.integrationLock = true;
            $scope.info.integrationMsg = '请输入url并查询后修改集成时间';
            $scope.info.nextLock = true;
            return false;
        } else {


        }
    };
    $scope.updateOnlineDate = function () {

        if ($scope.info.PlanInfo.length <= 0) {
            $scope.info.online_date = "";
            $scope.info.online_dateLock = true;
            $scope.info.online_dateMsg = '请输入url并查询后修改上线时间';
            $scope.info.nextLock = true;
            return false;
        } else {

            if ($scope.info.integration_date > $scope.info.online_date) {
                $scope.info.online_dateLock = true;
                $scope.info.online_dateMsg = '上线日期大于集成日期';
                $scope.info.nextLock = true;
            } else {
                $scope.info.online_dateLock = false;
                $scope.info.online_dateMsg = '';
                $scope.info.nextLock = false;
            }
        }
    };

    $scope.getLoad_level = function () {
        $http.get(ucloud_master_url + '/api/v1/load_level/?token_id=' + getTokenId()).success(function (data) {

            $scope.info.load_level = data.data;

            $http.get(ucloud_master_url + '/api/v1/agent/?token_id=' + getTokenId() + '&is_only_free=true').success(function (data) {
                $scope.info.agent_List = data.data;
                angular.forEach($scope.info.load_level, function (load_level) {
                    load_level.mun = 0;
                    angular.forEach($scope.info.agent_List, function (agent_List) {
                        if (load_level.level_name == agent_List.level_name) {
                            load_level.mun++;
                        }

                    })
                })
            })

        })
    };

    $scope.previousBtn = function () {

        if ($scope.info.index == 3) {
            $scope.info.nextLock = false;
            $scope.info.branch_numLock = false;
        }
        if ($scope.info.index > 1) {
            if($scope.info.planType == '1' && $scope.info.index == 5){
                $scope.info.index = 3;
            }else {
                $scope.info.index = $scope.info.index - 1;
            }
        } else {
            $scope.info.index = 1;
        }
    };

    $scope.queryUrl = function () {

        if ($scope.info.PlanInfoByUrl == '') {
            alert('请输入url！');
            return false;
        }

        LoadModal($uibModal);
        $http.get(beetle_plan_url + '/zentao/getProjectPlanInfoByUrl?token_id=' + getTokenId() + '&zentao_url=' + encodeURIComponent($scope.info.PlanInfoByUrl)).success(function (data) {
            closeModal()
            if (data.success == true) {
                $scope.info.PlanInfo = data.data;
                $scope.info.realnames = '';
                $scope.info.teamMembers = [];
                angular.forEach(data.data.members, function (members) {
                    $scope.info.realnames += members.realname + ' ';
                    $scope.info.teamMembers.push(members);
                });
                if ($scope.info.PlanInfo.isIdExist == true) {
                    $scope.info.nextLock = true;
                } else {
                    $scope.info.nextLock = false;
                }

                // var end=new Date($scope.info.PlanInfo.end).getTime();
                var oneDayLong = 24 * 60 * 60 * 1000;
                var end = new Date($scope.info.PlanInfo.end);
                var endTime = end.getTime();
                var endDay = end.getDay();
                var MondayTime = endTime - (endDay - 1) * oneDayLong;
                var SundayTime = endTime + (5 - endDay) * oneDayLong;
                var oneWeekTime = endTime + oneDayLong * 7;
                $scope.info.integration_date = new Date(MondayTime);
                $scope.info.integrationLock = false;
                $scope.info.online_date = new Date(SundayTime);
                $scope.info.online_dateLock = false;
                $scope.info.expect_recover_date = new Date(oneWeekTime);
                $scope.info.one = true;
                $scope.info.checkplan_code = $scope.info.PlanInfo.code;

            } else {
                alert(data.msg);
            }
        }).error(function (data) {
            alert(data.message);
            closeModal()
        })

    };
    $scope.testCode = function () {
        var testStr = /^([a-z]|[A-Z])+([a-z]|[A-Z]|\d|_|)*$/;
        var code = $scope.info.checkplan_code;
        if (code.match(testStr)) {
            $scope.info.branch_numLock = false;
            $scope.info.codeMsg = '';
        } else {
            $scope.info.branch_numLock = true;
            $scope.info.codeMsg = '请输入正确的项目代号！'
        }
    };

    $scope.checkProjectPlanCode = function () {
        if ($scope.info.checkplan_code == '') {
            alert("请输入项目代号!");
            return;
        }
        $scope.msg = '';
        LoadModal($uibModal);
        $http.get(beetle_plan_url + '/project_plan/checkProjectPlanCode?token_id=' + getTokenId() + '&plan_code=' + $scope.info.checkplan_code + '&project_names=' + JSON.stringify($scope.info.trunk_list)).success(function (data) {
            closeModal();
            if (data.success == true) {
                // $scope.info.ProjectPlanCodelock=true;
                $scope.info.nextLock = false;
                angular.forEach($scope.info.ProjectDefaultNameList, function (NameList) {
                    NameList.msg = "";

                })

            } else {
                $scope.info.nextLock = true;

                if (data.msg != "") {
                    $scope.msg = data.msg;
                }
                // $scope.info.ProjectPlanCodelock=false;
                $scope.info.nextLock = true;
                angular.forEach($scope.info.ProjectDefaultNameList, function (NameList) {
                    NameList.msg = "";
                    angular.forEach(data.data, function (msgs) {
                        if (msgs.name == NameList.trunk_name) {
                            NameList.msg = msgs.msg
                        }
                    })
                })
            }
        }).error(function (data) {
            alert(data.message);
            closeModal()
        })

    };
    $scope.getPlanProjectDefaultName = function () {
        LoadModal($uibModal);
        $http.get(beetle_plan_url + '/project_plan/getPlanProjectDefaultName?token_id=' + getTokenId() + '&trunk_list=' + JSON.stringify($scope.info.trunk_list) + '&plan_code=' + $scope.info.checkplan_code).success(function (data) {
            closeModal();
            if (data.success == true) {
                $scope.info.ProjectDefaultNameList = data.data;
                // $scope.checkProjectPlanCode();
            } else {
                alert(data.msg)
            }
        }).error(function (data) {
            alert(data.message);
            closeModal()
        })
    };

    $scope.vcs_path = function (i) {

        i.vcs_path = i.trunk_name + '-' + i.branch_num + '.' + $scope.info.checkplan_code;
    };
    $scope.getZoneList = function () {
        LoadModal($uibModal);
        $scope.info.zones = [];
        $http.get(ucloud_master_url + '/api/v1/agent/getZoneList?token_id=' + getTokenId()).success(function (data) {
            closeModal();
            $scope.info.ZoneList = data.data;
            angular.forEach($scope.info.ZoneList, function (data) {
                if (data.zone_title == '深圳仓' || data.zone_title == '中国区' || data.zone_title == '美西仓') {
                    $scope.info.zones.push(data)
                }
            })

        }).error(function (data) {
            alert(data.message);
            closeModal()
        })

    };
    $scope.branch_num = function (i) {

        var branch_num = i.branch_num;

        var t = /^(\d{1,4}){1}(\.\d{1,4}){2}$/;
        if (branch_num.match(t)) {
            $scope.info.branch_numLock = false;
            return i.branch_numMsg = '';
        } else {
            $scope.info.branch_numLock = true;
            return i.branch_numMsg = '请输入正确的版本号！'
        }

    };

    $scope.edit = function (object) {
        if (!object.modifiable) {
            object.modifiable = true;
            return;
        } else {
            object.modifiable = false;

        }
    };

    $scope.goOa = function () {
        window.open('http://oa.winit.com.cn')
    };

    $scope.goInit = function () {
        var info = {};
        info = $scope.info;
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'subProject.html',
            controller: 'subProjectCtrl',
            keyboard: false,
            backdrop: 'static',
            resolve: {
                info: function () {
                    return info;
                }
            }
            // size: size
            /*,
             resolve: {
             items: function () {
             return $scope.items;
             }
             }*/
        });
    };

});


myApp.controller('subProjectCtrl', function ($scope, $uibModalInstance, info, $http, $filter, $uibModal, ngDialog) {
    $scope.info = info;
    $scope.info.immediately = false;


    $scope.save = function () {

        var uCloud = [];
        if ($scope.info.DEV) {
            uCloud.push('DEV');
        }
        if ($scope.info.QA) {
            uCloud.push('QA')
        }
        if ($scope.info.UAT) {
            uCloud.push('UAT')
        }
        var data = {
            token_id: getTokenId(),
            plan_code: $scope.info.checkplan_code,
            plan_name: $scope.info.PlanInfo.name,
            online_date: $filter('date')($scope.info.online_date, "yyyy-MM-dd"),
            isUseWeb: $scope.info.isUseWeb,
            useOldEnv: $scope.info.useOldEnv
        };
        if ($scope.info.ProjectDefaultNameList.length > 0) {
            data.projects = JSON.stringify($scope.info.ProjectDefaultNameList);
        }
        if (uCloud.length > 0) {
            data.ucloud = JSON.stringify(uCloud)
        }
        if ($scope.info.title_list.length > 0) {
            data.zones = JSON.stringify($scope.info.title_list)
        }
        if ($scope.info.useWebDesc != '') {
            data.useWebDesc = $scope.info.useWebDesc
        }
        if ($scope.info.expect_recover_date != "") {
            data.recover_date = $filter('date')($scope.info.expect_recover_date, "yyyy-MM-dd")
        }
        var url = beetle_plan_url + '/project_plan';
        if ($scope.info.planType == '0') {
            data.end_date = $scope.info.PlanInfo.end;
            data.pm_name = $scope.info.PlanInfo.PM_name;
            data.integration_date = $filter('date')($scope.info.integration_date, "yyyy-MM-dd");
            data.zt_project_id = $scope.info.PlanInfo.id;
            data.teamMembers = JSON.stringify($scope.info.teamMembers);
            data.start_date = $scope.info.PlanInfo.begin;
            data.pm = $scope.info.PlanInfo.PM;
            url = url + "/createProjectPlan";
        } else if ($scope.info.planType == '1') {
            data.bug_num = $scope.info.bug_num;
            data.need_num = $scope.info.need_num;
            data.test_head = $scope.info.testUserInfo.account;
            data.test_head_name = $scope.info.testUserInfo.realname;
            url = url + "/createEmergencyProjectPlan";
        }
        doBeetlePost($http, $uibModal, url, data, function (data) {
            if (data.success) {
                if ($scope.info.planType == "1") {
                    window.location.href = "#/menu/emergencyList/";
                } else {
                    window.location.href = "#/menu/planList/";
                }
                $scope.cancel();
            } else {
                openDialogAlert(ngDialog, $scope, data.msg);
            }
        });
    };

    $scope.cancel = function () {
        // 关闭弹出框
        $uibModalInstance.dismiss('cancel');
    };

});